//
//  CommentViewController.m
//  giuseppe
//
//  Created by 王晨辉 on 15/8/24.
//  Copyright (c) 2015年 GiantUmbrella. All rights reserved.
//

#import "CommentViewController.h"

#import "addImageView.h"
#import "Helper.h"


@interface CommentViewController ()<UITextViewDelegate,MBProgressHUDDelegate>{
    
    
    addImageView *addImage;
     MBProgressHUD *HUD;
}

@end

@implementation CommentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = UIColorWithRGBA(218, 218, 218, 1);

    //自定义的导航栏
    UIView *navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 64)];
    navView.backgroundColor = [UIColor whiteColor];
    navView.alpha = 0.90;
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(10, 33, 27, 16);
    [backButton setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth/2-50, 20, 100, 40)];
    titleLabel.text = @"编辑评论";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    

    UIButton *sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
    sendButton.frame = CGRectMake(kScreenWidth-60, 30, 60, 30);
    [sendButton setTitle:@"完成" forState:UIControlStateNormal];
    [sendButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [sendButton addTarget:self action:@selector(send) forControlEvents:UIControlEventTouchUpInside];
    
    [self subView];
    [navView addSubview:backButton];
    [navView addSubview:titleLabel];
    [navView addSubview:sendButton];
    [self.view addSubview:navView];
    

    
}
-(void)subView{
    
    
    _textview = [[UITextView alloc] initWithFrame:CGRectMake(0,64, kScreenWidth, 150)];
    //    _textview.layer.cornerRadius = 8;
    //    _textview.layer.masksToBounds = YES;
    UIColor *customColor  = UIColorWithRGBA(218, 218, 218, 1);
    _textview.layer.borderColor = customColor.CGColor;
    _textview.layer.borderWidth = 2.0;
    _textview.font = [UIFont systemFontOfSize:15];
    //textview.keyboardType = UIKeyboardTypeDefault;
    _textview.delegate = self;
    [self.view addSubview:_textview];

    
    
    ReasonIslab = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 200, 20)];
    ReasonIslab.enabled = NO;
    
    ReasonIslab.text = @"说点什么吧~";
    ReasonIslab.font =  [UIFont systemFontOfSize:15];
    ReasonIslab.textColor = [UIColor lightGrayColor];
    [_textview addSubview:ReasonIslab];

    
    addImage = [[addImageView alloc]initWithFrame:CGRectMake(0, _textview.frame.origin.y+_textview.frame.size.height+10, [UIScreen mainScreen].bounds.size.width, 95)];
    
    
    [self.view addSubview:addImage];
    
}

- (void) textViewDidChange:(UITextView *)textView{
    if ([textView.text length] == 0) {
        [ReasonIslab setHidden:NO];
    }else{
        [ReasonIslab setHidden:YES];
    }
}

- (void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    [self.view endEditing:YES];
    
}


- (void)send{
    
   
    HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    HUD.delegate = self;
    HUD.mode = MBProgressHUDModeIndeterminate;
    HUD.labelText = @"发送中";

    NSString *brief = _textview.text;
    
    //请求参数
    NSDictionary *paras= @{@"userId":@"1",
                           @"suitId":self.suitId,
                           @"brief":brief,
                           @"orderId":self.orderId
                           };
    //调用请求
    [[myDataAccess shardWebUtil] orderCommentsWithURLString:kNetPath_Code_productComments  andParas:paras andTransferGottenData:^(id obj, NSError *error)  {
        
        NSLog(@"%@",obj);
        
        if (obj) {
   
            NSString *result = [obj objectForKey:@"resultCode"];
            
            if (result==0) {
                
                HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
                HUD.mode = MBProgressHUDModeCustomView;
                HUD.labelText = @"发送成功";
            
                
                [self hudWasHidden:HUD];
                
                [self.navigationController popViewControllerAnimated:YES];
                
                }
                
      

        }else{
            
            //没数据
            
        }

    }];
    
    
    
}



#pragma mark  MBProgressHUD delegate
-(void)hudWasHidden:(MBProgressHUD *)hud
{
    [HUD removeFromSuperview];
    HUD = nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
